--
-- NewDailyTaskRender.lua
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019/1/3
-- 两个Res.TaskSideItemSubNode合一起

local ClassRef = app.mvc.createRenderClass(...)
local NewDailyTaskSubRender = import(".NewDailyTaskSubRender")

local table_insert = table.insert

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskDoubleSideItemSubNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())

    local bindPanel = {0, 0}
    for i = 1, #bindPanel do
        local panel = NewDailyTaskSubRender.create(self._ui["Panel/Panel_" .. i])
        panel:onBtnClick(function(op, data) executeFunc(self._onClick, op, data) end)
        bindPanel[i] = panel
    end
    self._bindPanel = bindPanel

    self:refresh()
end

function ClassRef:setDataWithIndex(task, index)
    if index > #self._bindPanel then return end
    local panel = self._bindPanel[index]
    if panel then
        panel:setData(task)
    end

    if self._data then
        self._data[index] = task
    end
end

function ClassRef:getId()
    if not self._data then return end
    local data = self._data
    local t_taskId = {}
    for i = 1, #data do
        table_insert(t_taskId, data[i]:getId())
    end
    return t_taskId
end

function ClassRef:getPos()
    if not self._data then return end
    local data = self._data
    local t_taskPos = {}
    for i = 1, #data do
        table_insert(t_taskPos, i, data[i]:getPos())
    end
    return t_taskPos
end

function ClassRef:getRenderByIndex(index)
    if index > #self._bindPanel then return end
    return self._bindPanel[index]
end

function ClassRef:refresh()
    if not self._data then return end
    local data = self._data

    for i = 1, #data do
        local panel
        if i <= #self._bindPanel then
            panel = self._bindPanel[i]
        end

        if panel then
            panel:setData(data[i])
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
